﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

public partial class cms_cmsColorSite : System.Web.UI.Page
{
    string aa = null;
    static string[] parts;
    private List<string> styleSheets;
    private SortedList<string, StyleClass> scc;

    public SortedList<string, StyleClass> Styles
    {
        get { return this.scc; }
        set { this.scc = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadCssFile();
            CssParser();
            GetStyleSheet(Server.MapPath("~/css/Site.css"));
        }
    }

    private void LoadCssFile()
    {
        string text = "";
        StreamReader stream = new StreamReader(Server.MapPath("~/css/Site.css"));
        if (stream != null)
        {
            string a = stream.ReadLine();
            while (a != null)
            {
                text += a + "</br>";
                a = stream.ReadLine();
            }
        }
        stream.Close();
        stream.Dispose();
        //Literal1.Text = text;
    }

    //To parse Css file.
    public void CssParser()
    {
        this.styleSheets = new List<string>();
        this.scc = new SortedList<string, StyleClass>();
    }

    //Get Style sheet to parse
    public void GetStyleSheet(string path)
    {
        this.styleSheets.Add(path);
        ParseStyleSheet(path);
    }
    public string GetStyleSheet(int index)
    {
        return this.styleSheets[index];
    }

    //Parsing style sheet
    private void ParseStyleSheet(string path)
    {
        string content = CleanUp(File.ReadAllText(path));
        parts = content.Split('}');

        for (int i = 0; i < parts.Length - 1; i++)
        {
            string[] parts1 = parts[i].Split('{');
            string str = parts1[0].ToString().Remove(0, 0);
            aa = aa + str + ",";
        }
        int len = aa.LastIndexOf(',');
        string ss = aa.Remove(len);
        string ss1 = aa.Remove(len - 1);
        string[] parts2 = ss.Split(',');

        //Binding all class of a css file to a drop down list
        DDLCssClass.DataSource = parts2;
        DDLCssClass.DataBind();
        lstClassProperty.Items.Clear();

        //Bind all property of a selected css class
        GetCssClassProperty(parts, DDLCssClass.SelectedValue);
    }

    private string CleanUp(string s)
    {
        string temp = s;
        string reg = "(/\\*(.|[\r\n])*?\\*/)|(//.*)";
        Regex r = new Regex(reg);
        temp = r.Replace(temp, "");
        temp = temp.Replace("\r", "").Replace("\n", "");
        return temp;
    }

    public class StyleClass
    {
        private string _name = string.Empty;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        private SortedList<string, string> _attributes = new SortedList<string, string>();
        public SortedList<string, string> Attributes
        {
            get { return _attributes; }
            set { _attributes = value; }
        }
    }
    protected void DDLCssClass_SelectedIndexChanged(object sender, EventArgs e)
    {
        lstClassProperty.Items.Clear();
        GetCssClassProperty(parts, DDLCssClass.SelectedValue);
    }

    //To Bind all property of a css class
    private void GetCssClassProperty(string[] classArray, string className)
    {
        for (int i = 0; i < classArray.Length - 1; i++)
        {
            string[] parts1 = classArray[i].Split('{');
            if ("." + className == parts1[0].ToString())
            {
                string[] parts3 = parts1[1].Split(';');
                for (int j = 0; j < parts3.Length - 1; j++)
                {
                    lstClassProperty.Items.Add(parts3[j].ToString());
                }
            }
        }
    }
}